/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.device;

import com.cxhd.lis.util.Tool;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 52461
 */
public class UT_150 {
    
    private String baseData;
    private String device_code = "uritest-150";
    private String code = "LN";
         private String sampletype = "尿液";
    private String sampletypeId = null;

    public UT_150(String baseData) {
        this.baseData = baseData;
        parseComData();
       // getSampleTypeId();
    }
    
    
      public void parseComData() {

        String sampleNo = null;
        String testDate = null;
        String testTime = null;
        baseData = baseData.replace("Sample Report  ", "").replace("  End", "");
        String strs[] = baseData.split(" ");
        Map<String, String> map = new HashMap<String, String>();
        List<String> list = new ArrayList<String>();
        String key = "";
        System.out.println(strs.length);
        for (String s : strs) {

            if (s.trim().length() > 0) {
                list.add(s);
            }

        }

        for (int i = 0; i < list.size(); i++) {
            if (i % 2 == 0) {
                key = list.get(i);
            } else if (key.equalsIgnoreCase("Date")) {
                testDate = list.get(i);
            } else if (key.equalsIgnoreCase("Time")) {
                testTime = list.get(i);
            } else if (key.equalsIgnoreCase("PAT")) {
                sampleNo = list.get(i);
            } else if (!key.equalsIgnoreCase("reel") && !key.equalsIgnoreCase("cup")) {
                map.put(key, list.get(i));
            }
        }
      
        sendDb(Tool.getSampleNo(code, sampleNo), testDate + " " + testTime, map);
    }

    

    public void sendDb(String sampleNo, String datetime, Map<String, String> result) {
        
        System.out.println(sampleNo + "  " + datetime);
       // System.out.println(result);

        for (String key : result.keySet()) {
            try {
                System.out.println("保存到数据库");
                makeInsertSqlInsert(tranMap(sampleNo, datetime, key, result.get(key)), "lis_testresult");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        
    }

    //转换数据结构
    public Map<String, String> tranMap(String sampleNo, String datetime, String key, String value ) {
        try {
            Map<String, String> newMap = new HashMap<String, String>();

             Record rs = Db.findFirst("select * from lis_testdescribe where TESTID='" + key.trim().replace("*", "") + "'");
            if (rs != null) {
                newMap.put("ITEMPK", rs.get("PK")+"");
                newMap.put("TESTID", rs.get("TESTID")+"");
                newMap.put("UNIT", rs.get("UNIT")+"");
                newMap.put("SAMPLENO", sampleNo);

                newMap.put("TESTRESULT", value);
                newMap.put("MEASURETIME", datetime);
                newMap.put("DEVICEID", device_code);
                newMap.put("SAMPLETYPE", "16");
                newMap.put("UUID", Tool.string2MD5(datetime.trim() + sampleNo.trim() + device_code));
                return newMap;

            }
            return null;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    //构建sql
    private boolean makeInsertSqlInsert(Map<String, String> map, String tableName) throws Exception {

        if (map == null) {
            return false;
        }

        String sql = "";
        StringBuilder sqlHead = new StringBuilder();
        StringBuilder sqlvalue = new StringBuilder();
        int index = 0;
        String[] values = new String[map.size()];
        sqlHead.append("insert into ").append(tableName).append(" (");
        sqlvalue.append(" values (");
        for (String key : map.keySet()) {
            values[index] = map.get(key);
            index++;
            if (index == map.size()) {
                sqlHead.append("`").append(key).append("`").append(" )");
                sqlvalue.append(map.get(key) == null ? "null)" : "'" + map.get(key) + "')");
                // map.get(key)==null?"":"";
                // sqlvalue.append("? )");
            } else {
                sqlHead.append("`").append(key).append("`").append(" , ");
                sqlvalue.append(map.get(key) == null ? "null," : "'" + map.get(key) + "',");
                //sqlvalue.append("?,");
            }

        }
        sql = sqlHead.append(sqlvalue).toString();
        //System.out.println(sqlHead.append(sqlvalue).toString()+"  "+values.length);
        //logger.info(sql);
        try {
          Db.update(sql);
        } catch (Exception e) {
            System.out.println(e);
        }
        return true;

    }
    
    
}
